package org.jeecg.modules.jd.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.jd.entity.JD_order_sku;
import org.jeecg.modules.jd.entity.JD_position;
import org.jeecg.modules.jd.service.IJD_goods_categoryService;
import org.jeecg.modules.jd.service.IJD_order_skuService;
import org.jeecg.modules.jd.service.IJD_positionService;
import org.jeecg.modules.jd.util.JDGoodsRequest;
import org.jeecg.modules.jd.util.JDJingFenGoodRequest;
import org.jeecg.modules.jd.util.JDGoodsUtil;
import org.jeecg.modules.mall.entity.TbMnMaterialOptional;
import org.jeecg.modules.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 描述
 * </p>
 */
@Slf4j
@Api(tags = "app京东")
@RestController
@RequestMapping("/app/jd/api")
public class JDController {


    @Autowired
    private ITbMnUserService tbMnUserService;

    @Autowired
    private IJD_positionService jD_positionService;

    @Autowired
    private IJD_goods_categoryService jD_goods_categoryService;

    @Autowired
    private IJD_order_skuService jD_order_skuService;


//    @AutoLog(value = "京东类目-初始化")
//    @ApiOperation(value = "京东类目-初始化", notes = "京东类目-初始化")
//    @PostMapping(value = "/initData")
//    public Result<?> initData() {
//        jD_goods_categoryService.initData();
//        return Result.ok("初始化成功！");
//    }


    @AutoLog(value = "京东订单")
    @ApiOperation(value = "京东订单")
    @PostMapping(value = "/getJDOrders")
    public Result<?> getJDOrders(String user_id, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, JD_order_sku jd_order_sku) {

        Page<JD_order_sku> page = new Page<>(pageNo, pageSize);
        return jD_order_skuService.pageJDOrders(user_id,page,jd_order_sku);
    }

    @AutoLog(value = "京东类目-获取")
    @ApiOperation(value = "京东类目-获取", notes = "京东类目-获取")
    @PostMapping(value = "/getGoodsCatgorys")
    public Result<?> getGoodsCatgorys() {
        return jD_goods_categoryService.getGoodsCatgorys();
    }


    @AutoLog(value = "京东京粉精选商品-精选")
    @ApiOperation(value = "京东京粉精选商品-精选")
    @PostMapping(value = "/queryJingfenGoods")
    public Result<?> queryJingfenGoods(JDJingFenGoodRequest jdJingFenGoodRequest) {

        return jD_goods_categoryService.getJingfenGoodsData(jdJingFenGoodRequest);
    }

    @AutoLog(value = "京东类目商品查询")
    @ApiOperation(value = "京东类目商品查询")
    @PostMapping(value = "/queryGoodsByCatgory")
    public Result<?> queryGoodsByCatgory(JDGoodsRequest jdGoodsRequest) {
        return jD_goods_categoryService.getGoodsData(jdGoodsRequest);
    }


    @AutoLog(value = "转链")
    @ApiOperation(value = "转链")
    @PostMapping(value = "/zhuanlian")
    public Map<String, Object> zhuanlian(String materialId, String couponUrl, String user_id) {
        Map<String, Object> resultmap = new HashMap<>();
        if (StringUtils.isEmpty(materialId) || StringUtils.isEmpty(couponUrl) || StringUtils.isEmpty(user_id)) {
            resultmap.put("code", 400);
            resultmap.put("msg", "参数错误");
            resultmap.put("success", false);
            return resultmap;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user_id);
        JD_position jd_position = jD_positionService.getOne(queryWrapper);
        String json = JDGoodsUtil.zhuanlian(materialId, jd_position.getId(), couponUrl);
        resultmap.put("code", 200);
        resultmap.put("success", true);
        resultmap.put("data", json);
        return resultmap;
    }


    @AutoLog(value = "超级搜索产品列表")
    @ApiOperation(value = "超级搜索产品列表", notes = "超级搜索产品列表")
    @PostMapping(value = "/superQueryList")
    public Map<String, Object> superQueryList(JDJingFenGoodRequest jdGoodRequest) {
        Map<String, Object> resultmap = new HashMap<>();
        log.info("京东产品列表,JDGoodRequest:{}", jdGoodRequest.toString());
        String json = "";
        try {
            if (null == jdGoodRequest.getUser_id()) {
                resultmap.put("code", 400);
                resultmap.put("msg", "用户信息没有");
                resultmap.put("success", false);
                return resultmap;
            }
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", jdGoodRequest.getUser_id());
            JD_position jd_position = jD_positionService.getOne(queryWrapper);
            jdGoodRequest.setJd_position_id(jd_position.getId());
            Map<String, Object> data = JDGoodsUtil.getSuperQueryList(jdGoodRequest);
            log.info("京东产品列表,JDGoodRequest:{}", jdGoodRequest.toString());
            resultmap.put("code", 200);
            resultmap.put("success", true);
            resultmap.put("data", data);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultmap;
    }


}
